#include "edituserdialog.h"
#include "ui_edituserdialog.h"
#include "video_playback.h"
#include <QValidator>
#include <QRegularExpression>

EditUserDialog::EditUserDialog(QWidget *parent, const QString& text)
    : QDialog(parent)
    , ui(new Ui::EditUserDialog)
{
    ui->setupUi(this);

    // 去除窗口边框
    setWindowFlag(Qt::FramelessWindowHint);
    // 设置窗口透明
    setAttribute(Qt::WA_TranslucentBackground);
    // 设置窗口描述
    ui->titleLabel->setText(text);

    // 移动窗口位置，和主界面左上角对齐--Video_playback
    Video_playback* video_playback = Video_playback::getInstance();
    this->move(video_playback->mapToGlobal(QPoint(0, 0)));

    // 对phoneEdit中的手机号进行限制, 以1开始的11整形数字
    QRegularExpression regExp("^1\\d{10}$");
    QValidator* validator = new QRegularExpressionValidator(regExp, this);
    ui->phoneEdit->setValidator(validator);

    // 设置管理员
    ui->roleComboBox->addItem("平台管理员");
    ui->roleComboBox->setCurrentIndex(0);

    // 编辑框内容改变
    connect(ui->commentTextEdit, &QPlainTextEdit::textChanged, this, [=]{
        QString text = ui->commentTextEdit->toPlainText();
        int wordCount = text.length();
        if(wordCount > 10)
        {
            wordCount = 10;
            ui->commentTextEdit->setPlainText(text.mid(0, 10));
        }
        ui->wordCount->setText(QString::number(wordCount) + "/10");

        QTextCursor textCursor = ui->commentTextEdit->textCursor();
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        ui->commentTextEdit->setTextCursor(textCursor);
    });

    // 取消按钮点击
    connect(ui->cancelBtn, &QPushButton::clicked, this, &EditUserDialog::onCancelBtnClicked);
    connect(ui->submitBtn, &QPushButton::clicked, this, &EditUserDialog::onSubmitBtnClicked);
}

EditUserDialog::~EditUserDialog()
{
    delete ui;
}

void EditUserDialog::onSubmitBtnClicked()
{

}

void EditUserDialog::onCancelBtnClicked()
{
    close();
}
